package com.soft.mainsite.module.web.cms;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.soft.mainsite.common.constant.CmsConst;
import com.soft.mainsite.module.web.cms.service.CategoryService;
import com.soft.mainsite.module.web.cms.service.TopicService;
import com.soft.mainsite.common.base.BaseController;
import com.soft.mainsite.common.utils.CmsUtil;
import com.soft.mainsite.common.utils.ControllerUtil;
import com.soft.mainsite.module.web.system.vo.UserVo;
import com.soft.mainsite.mybatis.model.TCmsCategory;
import com.soft.mainsite.mybatis.model.TCmsTopic;
import org.apache.shiro.authz.UnauthenticatedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Description:专题控制器
 *
 * @author Jin
 * @create 2017-07-19
 **/
@Controller
@RequestMapping("/system/cms/topic")
public class TopicController extends BaseController<TCmsTopic> {

    @Autowired
    private TopicService topicService;

    @Autowired
    private CategoryService categoryService;

    @Override
    @RequestMapping
    public String index(@RequestParam(value = "pageCurrent", defaultValue = "1") Integer pageNumber,
                        @RequestParam(value = "pageSize", defaultValue = "50") Integer pageSize,
                        TCmsTopic pojo, Model model) {
        UserVo userVo = ((UserVo) ControllerUtil.getHttpSession().getAttribute(CmsConst.SITE_USER_SESSION_KEY));
        if (CmsUtil.isNullOrEmpty(userVo)) {
            throw new UnauthenticatedException();
        }
        pojo.setSiteId(userVo.getSiteId());
        model.addAttribute("pojo", pojo);
        model.addAttribute("model", topicService.page(pageNumber, pageSize, pojo));
        return "cms/topic_list";
    }

    @Override
    @RequestMapping("/input")
    public String input(@RequestParam(value = "id", required = false) Integer id,
                        Model model) {
        if (id != null) {
            model.addAttribute("topic", topicService.findById(id));
        }
        return "cms/topic_input";
    }

    @Override
    @RequestMapping("/save")
    @ResponseBody
    public String save(TCmsTopic pojo) {

        String categorys = pojo.getCategoryIds();

        if (StrUtil.isNotBlank(categorys)) {

            String[] categorysArray = categorys.split(",");
            List<TCmsCategory> tCmsCategories = new ArrayList<>(categorysArray.length);

            Arrays.asList(categorysArray).forEach(item -> {
                TCmsCategory category = categoryService.findById(Long.parseLong(item));
                tCmsCategories.add(category);
            });

            List<TCmsCategory> collect = tCmsCategories.stream().
                    sorted((s1, s2) -> s1.getSortId().compareTo(s2.getSortId())).
                    collect(Collectors.toList());

            List<Long> categorysStr = new ArrayList<>();

            collect.forEach(item -> {
                categorysStr.add(item.getCategoryId());
            });

            pojo.setCategoryIds(CollectionUtil.join(categorysStr, ","));
        }

        if (pojo.getTopicId() != null) {
            return topicService.update(pojo);
        }
        return topicService.save(pojo);
    }

    @RequestMapping("/delete")
    @ResponseBody
    @Override
    public String delete(@RequestParam(value = "ids", required = false) Integer[] ids) {
        return topicService.delete(ids);
    }

    /**
     * 查询专题列表
     *
     * @param
     * @return 专题列表
     * @author 胡佳明
     * @date 2019/5/9 17:43
     */
    @RequestMapping("/list")
    public String topicList(@RequestParam(value = "pageCurrent", defaultValue = "1") Integer pageNumber,
                            @RequestParam(value = "pageSize", defaultValue = "50") Integer pageSize,
                            TCmsTopic pojo, Model model) {
        UserVo userVo = ((UserVo) ControllerUtil.getHttpSession().getAttribute(CmsConst.SITE_USER_SESSION_KEY));
        if (CmsUtil.isNullOrEmpty(userVo)) {
            throw new UnauthenticatedException();
        }
        pojo.setSiteId(userVo.getSiteId());
        model.addAttribute("pojo", pojo);
        model.addAttribute("model", topicService.pageList(pageNumber, pageSize, pojo));
        return "cms/topic_list";
    }

}
